package com.hu.web.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class HelloServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("HelloServlet 成功访问");

        // 向请求域中添加数据
        String msg = "服务器返回的数据";
        request.setAttribute("msg", msg);
        request.setAttribute("url", "https://www.baidu.com");
        request.setAttribute("names", new String[]{"java", "mysql", "oracle"});
        request.setAttribute("list", Arrays.asList("a", "b", "c", "d"));

        // 向应用域中添加数据
        ServletContext application = request.getServletContext();
        application.setAttribute("applicationMsg", "这是应用域中的数据");

        // 使用 Thymeleaf 对页面进行渲染操作(不再使用原始的转发和重定向),但是其原理其实也是转发
        this.processTemplate("admin", request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
